package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.JobStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.EditBargainReq;
import com.ygqh.baby.po.*;
import com.ygqh.baby.service.YgBargainDetailService;
import com.ygqh.baby.service.YgBargainService;
import com.ygqh.baby.service.YgUserBargainDetailService;
import com.ygqh.baby.service.YgUserBargainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/bargain")
public class BargainController extends BaseController<Object, java.lang.Long> {

    @Autowired
    private YgBargainService ygBargainService;
    @Autowired
    private YgUserBargainService ygUserBargainService;
    @Autowired
    private YgBargainDetailService ygBargainDetailService;
    @Autowired
    private YgUserBargainDetailService ygUserBargainDetailService;

    @RequestMapping("edit")
    @ResponseBody
    public Message edit(@RequestBody EditBargainReq editBargainReq) {
        System.err.println(editBargainReq.toString());
        initEdit(editBargainReq);
        int i = ygBargainService.editBargain(editBargainReq);
        return Message.success(i);
    }

    @RequestMapping("search")
    @ResponseBody
    public Map<String, Object> search(String q, QueryInfo queryInfo, Date startDate, Date endDate, JobStatus jobStatus) {
        ResultSet<YgBargain> resultSet = ygBargainService.search(queryInfo, q, jobStatus, startDate, endDate);
        return resultSet.toDatatables(null);
    }

    @RequestMapping("searchUserBargainList")
    @ResponseBody
    public Map<String, Object> searchUserBargainList(QueryInfo queryInfo, Long userBargainId, String userName, String productName, Date startDate,
                                                     Date endDate, JobStatus jobStatus, String q) {
        ResultSet<YgUserBargain> resultSet = ygUserBargainService.search(queryInfo, userBargainId, userName, productName, startDate, endDate, jobStatus, q);
        return resultSet.toDatatables(null);
    }

    @RequestMapping("findById")
    @ResponseBody
    public Message findById(Long id) {
        YgBargain result = ygBargainService.findById(id);
        return Message.success(result);
    }

    @RequestMapping("findDetailById")
    @ResponseBody
    public Message findDetailById(Long id) {
        List<YgBargainDetail> result = ygBargainDetailService.findDetailByBargainId(id);
        return Message.success(result);
    }


    @RequestMapping("findProductForBargain")
    @ResponseBody
    public Message findProductForBargain(String productName, Long groupId, Long bargainId) {
        List<YgProduct> list = ygBargainService.findProductForBargain(null, productName, groupId, bargainId);
        return Message.success(list);
    }

    @RequestMapping("/findCutList")
    @ResponseBody
    public Message findCutList(Long userBargainId) {
        List<YgUserBargainDetail> list = ygUserBargainDetailService.findCutList(userBargainId);
        return Message.success(list);

    }

    @RequestMapping("/findProductListByBargainId")
    @ResponseBody
    public Message findProductListByBargainId(@RequestParam Long bargainId) {
        List<YgProduct> list = ygUserBargainDetailService.findProductListByBargainId(bargainId);
        return Message.success(list);

    }
}
